<?php

namespace Wechat\Controller;

class ClubfootController extends CommonController {


    public function index() {
        $this->assign("info", $this->customer);
        $this->display("index");
    }

    public function myIcoin() {
        $this->assign("info", $this->customer);
        $this->display("myIcoin");
    }

    public function faq() {

        $this->display("faq");
    }

    public function clubfootProductList() {

        $categoryId = 2;
        $map["p.category_id"] = $categoryId;

        $list = D("Product")->alias("p")
            ->where($map)
//            ->field("p.product_id,p.product_name,c.is_finished")
            ->select();

        echo json_encode($list);

    }

    public function ponsetiProductList() {

        $categoryId = 11;
        $map["p.category_id"] = $categoryId;

        $list = D("Product")->alias("p")
            ->where($map)
//            ->field("p.product_id,p.product_name,c.is_finished")
            ->select();

        echo json_encode($list);

    }

    public function FaqList() {

        $classId = I("post.classId");
        $keyword = I("post.keyword");

        if(!isNullOrEmpty($classId)) {
            $map["faq.faq_class_id"] = $classId;
        }
        if(!isNullOrEmpty($keyword)) {
            $map["faq.name|faq.question|faq.answer"] = array('like','%'.$keyword.'%');
        }

        $list = D("Faq")->alias("faq")
            ->join("left join __FAQ_CLASS__ fcl on fcl.faq_class_id = faq.faq_class_id")
            ->order("faq.faq_id asc")
            ->where($map)
            ->field("faq.*,fcl.faq_class_name, fcl.faq_class_type")
            ->select();

        echo json_encode($list);

    }

    public function faqDetail() {

        $id = $_REQUEST["id"];
        $info = D("Faq")->queryById($id);

//        if(isNullOrEmpty($info)) {
//            $this->redirect(U("product/index"));
//        }
//
        $info["count"] = $info["count"] + 1;
        D("Faq")->updateInfo($info);

        $this->assign("info", $info);
        $this->display("faqDetail");
    }

    public function checkin() {

        $map["customer_id"] = $this->customer["customer_id"];
        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',',$employee["job"]);
        $agentId = explode(',',$employee["agent_id"]);

        $list = D("ClubfootLog")->alias("cfl")
            ->where($map)
            ->order("cfl.checkin_time desc")
            ->select();

        $duration = 0;
        $lastEndTime = date("Y-m-d H:i:s");
        $status = $list[0]["checkin_type"];
        foreach($list as $check){
            if($check["checkin_type"] != $status){
                continue;
            }
            if($check["checkin_type"] == 1){
                if(strtotime($check["checkin_time"]) <= strtotime(date("Y-m-d 00:00:00"))){
                    $duration += (strtotime($lastEndTime) - strtotime(date("Y-m-d 00:00:00")));
                    break;
                }else{
                    $duration += (strtotime($lastEndTime) - strtotime($check["checkin_time"]));
                }
                $status = 0;
            }
            if($check["checkin_type"] == 0){
                if(strtotime($check["checkin_time"]) <= strtotime(date("Y-m-d 00:00:00"))){
                    break;
                }
                $lastEndTime = $check["checkin_time"];
                $status = 1;
            }
        }
        $hour =  floor($duration%86400/3600);
        $minute =  floor($duration%86400/60%60);
//        echo (floor($duration/60));
        if($list[0]["checkin_type"] == 1){
            $info["checkin_type"] = 1;
        }else{
            $info["checkin_type"] = 0;
        }
        $info["checkin_time"] = $list[0]["checkin_time"];
        $info["duration"] =  $hour."小时".$minute."分";

        $this->assign("customer", $this->customer);
        $this->assign("info", $info);
        $this->assign("job", $job);
        $this->assign("agentId", $agentId);
        $this->display("checkin");
    }

    public function chart() {

        $id = $_REQUEST["id"];
        if(isNullOrEmpty($id)){
            $map["customer_id"] = $this->customer["customer_id"];
        }else{
            $map["customer_id"] = $id;
        }
        $customerInfo = D("Customer")->queryById($map["customer_id"]);
        $info["nickname"] = $customerInfo["nickname"];

        $list = D("ClubfootLog")->alias("cfl")
            ->where($map)
            ->order("cfl.checkin_time asc")
            ->select();

        $duration = 0;

        $status = 0;
        $lastCheckTime = $list[0]["checkin_time"];
        $lastCheckdate = date("Y-m-d", strtotime($lastCheckTime));

        foreach($list as $check){
            if($check["checkin_type"] == $status){
                continue;
            }
            if($check["checkin_type"] == 0){
                while(strtotime($check["checkin_time"]) > strtotime(date("Y-m-d 23:59:59", strtotime($lastCheckdate)))){
                    $duration += (strtotime(date("Y-m-d 23:59:59", strtotime($lastCheckdate))) - strtotime($lastCheckTime));
                    $date = date("m/d/Y",strtotime($lastCheckdate));
                    $chartList[$date] = round($duration/3600, 1);
//                    $chartList[$lastCheckdate] = floor($duration%86400/3600);
                    $duration = 0;
                    $lastCheckdate = date("Y-m-d", strtotime($lastCheckdate)+1*24*60*60);
                    $lastCheckTime = $lastCheckdate;
                }
                if(strtotime($check["checkin_time"]) < strtotime(date("Y-m-d", strtotime($lastCheckdate)+1*24*60*60))){
                    $duration += (strtotime($check["checkin_time"]) - strtotime($lastCheckTime));
                }

                $status = 0;
            }

            if($check["checkin_type"] == 1){
                if(strtotime($check["checkin_time"]) > strtotime(date("Y-m-d 23:59:59", strtotime($lastCheckdate)))){
                    $date = date("m/d/Y",strtotime($lastCheckdate));
                    $chartList[$date] = round($duration/3600, 1);
//                    $chartList[$lastCheckdate] = round($duration/3600, 1);
                    $duration = 0;
                }
                $lastCheckTime = $check["checkin_time"];
                $lastCheckdate = date("Y-m-d", strtotime($lastCheckTime));
                $status = 1;
            }


        }
        if($lastCheckdate == date("Y-m-d")){
            if($status == 1){
                $duration += (strtotime(date("Y-m-d H:i:s")) - strtotime($lastCheckTime));
            }
            $date = date("m/d/Y",strtotime($lastCheckdate));
            $chartList[$date] = round($duration/3600, 1);
            $hour =  floor($duration%86400/3600);
            $minute =  floor($duration%86400/60%60);
        }else{
            if($status == 1){
                while($lastCheckdate < date("Y-m-d")){
                    $chartList[date("m/d/Y",strtotime($lastCheckdate))] = 24;
                    $lastCheckdate = date("Y-m-d", strtotime($lastCheckdate)+1*24*60*60);
                }
                if($lastCheckdate == date("Y-m-d")){
                    $duration = (strtotime(date("Y-m-d H:i:s")) - strtotime($lastCheckdate));
                    $date = date("m/d/Y");
                    $chartList[$date] = round($duration/3600, 1);
                }
                $hour =  floor($duration%86400/3600);
                $minute =  floor($duration%86400/60%60);
//        echo (floor($duration/60));
            }
        }

//        foreach($chartList as $chartKey => $chartValue) {
//            echo($chartKey.":".$chartValue);echo("---");
//        }

        $info["checkin_time"] = $list[0]["checkin_time"];
        $info["duration"] =  $hour."小时".$minute."分";

//        foreach($chartList as $chartKey => $chartValue) {
//            echo($chartKey.":".$chartValue);echo("<br/>");
//        }
        $chartJson = json_encode($chartList);
        $this->assign("info", $info);
        $this->assign("chartJson", $chartJson);
//        $this->assign("chartList", $chartList);
        $this->display("chart");
    }



    public function freshCheckin() {
        $map["customer_id"] = $this->customer["customer_id"];
        $list = D("ClubfootLog")->alias("cfl")
            ->where($map)
            ->order("cfl.checkin_time desc")
            ->select();
        $data["code"] = 0;
        if($list[0]["checkin_type"] == 1){
            $lastDuration = (strtotime(date("Y-m-d H:i:s"))-strtotime($list[0]["checkin_time"]))/3600;
            if($lastDuration >= 24){
                $info["checkin_time"] = date("Y-m-d H:i:s",strtotime("+1 day",strtotime($list[0]["checkin_time"])));
                $info["checkin_type"] = 0;
                $info["customer_id"] = $this->customer["customer_id"];
                D("ClubfootLog")->add($info);
                $data["code"] = 200;
            }
        }

        echo json_encode($data);
    }

    public function dailyCheckin() {

        $map["customer_id"] = $this->customer["customer_id"];
        $list = D("ClubfootLog")->alias("cfl")
            ->where($map)
            ->order("cfl.checkin_time desc")
            ->select();

        if($list[0]["checkin_type"] == 1){
            $info["checkin_type"] = 0;
        }else{
            $info["checkin_type"] = 1;
        }
        $info["customer_id"] = $this->customer["customer_id"];
        $info["checkin_time"] = getCurrentDate();
        D("ClubfootLog")->add($info);
//        if($this->customer["last_checkin"] != date("Y-m-d")){
//            $this->customer["checkin"] += 1;
//            $this->customer["last_checkin"] = date("Y-m-d");
//        }
//        if(isNullOrEmpty($this->customer["checkin_days"])){
//            $this->customer["checkin_days"] = date("d");
//        }else{
//            $this->customer["checkin_days"] .= ",".date("d");
//        }
        if($info["checkin_type"] == 1){
            $data["code"] = 100;
        }
        if($info["checkin_type"] == 0){
            $data["code"] = 200;
        }


        echo json_encode($data);
    }

    public function guarantee() {
        $customerId = $this->customer["customer_id"];

        $map["customer_id"] = $customerId;
//        $map["day"] = date("Y-m-d");
        $info = D("Guarantee")->queryByMap($map);

        $this->assign("customerId", $customerId);
        $this->assign("info", $info);
        $this->display("guarantee");

    }

    /**
     * 提交留言
     */
    public function updateGuarantee() {

        $guarantee = D("Guarantee")->queryById(I("post.guaranteeId"));

        $guarantee["customer_id"] = I("post.customerId");
        $guarantee["create_time"] = getCurrentDate();
        $guarantee["invoice_number"] = I("post.invoiceNumber");
        $guarantee["invoice_name"] = I("post.invoiceName");
        $guarantee["note"] = I("post.note");
        $guarantee["status"] = -1;

        $guaranteeId = $guarantee["guarantee_id"];
        if(!isNullOrEmpty($guaranteeId)){
            D("Guarantee")->updateInfo($guarantee);
        }else{
            $guaranteeId = D("Guarantee")->add($guarantee);
        }

        //推送预约信息到管理员
//        $notifyData["openid"] = array("otBeowu8bYi2HBRTMBWSIWzzkkbo","otBeowlAfkwtcoHunTHTDVQucZ_E","otBeowv0VZQORekZps57qRBc5d_0");
        //$notifyData["openid"] = "otBeowu8bYi2HBRTMBWSIWzzkkbo";
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "5" || $job[0] == "100") {//西钓客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
//            $notifyData["url"] = "http://xwkang.com/wechat/messagebox/viewnotify/id/" . $guaranteeId;
            $notifyData["first"] = "收到IOWA支具注册信息。";
            $notifyData["keyword1"] = "发票号：【" . I("post.invoiceNumber") . "】 购买人：【" . I("post.invoiceName") . "】";
            $notifyData["keyword2"] = $guarantee["note"];
            $notifyData["keyword3"] = $guarantee["create_time"];
            $notifyData["remark"] = "";
            messageboxNotify($notifyData);
        }
        $data["code"] = 200;

        echo json_encode($data);

    }

}